%   Licensed under the Apache License, Version 2.0 (the "License");
%   you may not use this file except in compliance with the License.
%   You may obtain a copy of the License at
%  
%       http://www.apache.org/licenses/LICENSE-2.0
%  
%   Unless required by applicable law or agreed to in writing, software
%   distributed under the License is distributed on an "AS IS" BASIS,
%   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%   See the License for the specific language governing permissions and
%   limitations under the License.
%  
%   $Revision$
%   $Date$
%   $Author$
%   $HeadURL$
%

% TO USE THIS Agent on its own[order doesn't matter]
%   -  Start the rl_glue executable socket server on your computer
%   -  Run the SkeletonEnvironment and SkeletonExperiment from a
%   different codec (Python, Java, C, Lisp should all be fine)
%   -  Load this agent like:
%       >> theAgent=polyathlon_agent()
%       >> runAgent(theAgent);
%   NOTE: Type CTRL-C to abort the connection.
%

function theAgent=polyathlon_agent()
    theAgent.agent_init=@polyathlon_agent_init;
    theAgent.agent_start=@polyathlon_agent_start;
    theAgent.agent_step=@polyathlon_agent_step;
    theAgent.agent_end=@polyathlon_agent_end;
    theAgent.agent_cleanup=@polyathlon_agent_cleanup;
    theAgent.agent_message=@polyathlon_agent_message;
end

function polyathlon_agent_init(taskSpecJavaString)
    taskSpec = parse_polyathlon_taskspec(taskSpecJavaString);
    pretty_print_reward_range(taskSpec);
end    

function theAction=polyathlon_agent_start(theObservation)
%This is a persistent struct we will use to store things
%that we want to keep around
    global agent_data_struct;
%TODO
%    theAction = org.rlcommunity.rlglue.codec.types.Action();
%	theAction.intArray=[round(rand(1))];

	%Make copies (using Java methods) of the observation and action
	%Store in our persistent struct
	agent_data_struct.lastAction=theAction.duplicate();
	agent_data_struct.lastObservation=theObservation.duplicate();	
end

function theAction=polyathlon_agent_step(theReward, theObservation)
	%This is a persistent struct we will use to store things
	%that we want to keep around
    global agent_data_struct;
%TODO
%    theAction = org.rlcommunity.rlglue.codec.types.Action();
%    theAction.intArray=[round(rand(1))];

    %Make copies (using Java methods) of the observation and action
    %Store in our persistent struct
    agent_data_struct.lastAction=theAction.duplicate();
    agent_data_struct.lastObservation=theObservation.duplicate();
end

function polyathlon_agent_end(theReward)
    % TODO
end

function returnMessage=polyathlon_agent_message(theMessageJavaObject)
%Java strings are objects, and we want a Matlab string
    inMessage=char(theMessageJavaObject);
	returnMessage='I don\''t know how to respond to your message';
    fprintf('Agent received message: ''%s'', returning message ''%s''\n',...
        inMessage, returnMessage);
end

function polyathlon_agent_cleanup()
    global agent_data_struct;
end